<template>
    <el-form ref="form" :model="formData" label-width="80px">
        <el-form-item label="学期名称" prop="">
            <el-input v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" prop="">
            <el-switch v-model="formData.state" class="ml-2" inline-prompt 
            style="--el-switch-on-color: #13ce66; --el-switch-off-color: #b1b3b8"
                active-text=" 已 启 用 "
                inactive-text=" 未 启 用 "
            />
        </el-form-item>
        <div style="text-align: center;margin-top: 20px;">
            <el-button style="margin-right: 50px;" @click="Close()">取消</el-button>
            <el-button type="primary" @click="SubmitForm()">确定</el-button>
        </div>
    </el-form>
</template>

<script setup>
import service from '@/request';
import { ElMessage } from 'element-plus';
import { defineProps, onBeforeMount, reactive } from 'vue'
// #region 显示提示
async function ShowTip(isSuccess, msg) {
    if (isSuccess == true) {
        ElMessage({ message: msg, type: 'success' })
    }
    else {
        ElMessage({ message: msg, type: 'error' })
    }
}
//#endregion

let props = defineProps(["Close", "transmitObj"])

let formData = reactive
({
    id: 0,
    name: '',
    state:false
})


async function SubmitForm() {
    if (formData.name == '') {
        ShowTip(false,'请先完成必要步骤')
        return
    }
    //添加
    if (props.transmitObj == null) {
        await service.post(`/Mangment/addSemester?name=${formData.name}&state=${formData.state}`).then(
            async res => {
                ShowTip(true,"操作成功")
            },
            error => {
                ShowTip(false,error)
            }
        )
    }
    //编辑
    else {
        await service.post("/Mangment/updateSemester",formData).then(
            async res => {
                ShowTip(true,"操作成功")
            },
            error => {
                ShowTip(false,error)
            }
        )
    }
    await props.Close()
}

onBeforeMount(() => {
    //如果是编辑操作就初始化
    if (props.transmitObj != null) {
        for (var p in props.transmitObj) {
            formData[p] = props.transmitObj[p]
        }
    }
})
</script>

<style scoped>
</style>